NOTES
thing1 = 'abacus'
thing2 = 'bobbin'
print(f'Thing 1 is {thing1}. Thing 2 is {thing2}.')
tmp = thing1
thing1 = thing2
thing2 = tmp
print(f'Thing 1 is {thing1}. Thing 2 is {thing2}.')
NOTES
thing1
and thing2
tmp
pointing to the same value as thing1
(abacus
)abacus
, thing1
can point to someone elsething2
(bobbin
)bobbin
, thing2
can point to someone elsetmp
(abacus
)thing1
and thing2
have swapped their values[]
¶fruits = ['plums', 'peaches', 'pears', 'persimmons']
fruits[0]
fruits[1]
fruits[3]
NOTES
0
[0]
fruits[7]
NOTES
IndexError
fruits = ['plums', 'peaches', 'pears', 'persimmons']
fruits[-1]
fruits[-3]
fruits[-4]
NOTES
fruits[len(fruits)-1], fruits[-1]
NOTES
range
¶for i in range(5):
print(i)
NOTES
0
) What numbers were NOT printed? (i.e. 5
)type(range(5))
list(range(5))
NOTES
range
returns...a range object. fruits = ['apples', 'apricots', 'avacados']
for index in range(len(fruits)):
print(fruits[index])
# Same as
for fruit in fruits:
print(fruit)
NOTES
range(len(fruits))
produce? Write them out.index
?index
, what does fruits[index]
print?list(range(4, 12))
NOTES
range(a, b)
(two arguments) starts at a inclusive and goes to b exclusive.list(range(4, 12, 2))
NOTES
range(a, b, c)
(three arguments) starts at a inclusive and goes to b exclusive with a step size of c.What python expression using range
would you use to create the following sequences?
1, 2, 3, 4, 5
0, 2, 4, 6, 8
-3, 0, 3
5, 4, 3, 2
NOTES
range(a, b, c)
(three arguments) starts at a inclusive and goes to b exclusive with a step size of c.list(range(1, 6))
list(range(0, 10, 2))
list(range(-3, 6, 3))
list(range(5, 1, -1))
bears = ['brown','black','polar','koala']
bears
bears[3]
bears[3] = 'panda'
bears[3]
bears
NOTES
.append()
, we can change the value a given index points to'panda'
) on the heap and the last slot now pointing to it'koala'
is still on the heap, just nothing is pointing to it at the momentdef swap_first_and_last(items):
"""Swap the first and last items in the input list. Modify the list in place."""
first = items[0]
items[0] = items[-1]
items[-1] = first
numbers = [1, 2, 3, 4, 5]
print(numbers)
swap_first_and_last(numbers)
print(numbers)
swap_first_and_last(numbers)
print(numbers)
NOTES
numbers
pointing to the listswap_first_and_last
with variable items
items
now points to same value as numbers
. numbers
continues to point to the list.items
, the function changes the references in the list.numbers
still points to the same list.Just because a function can change a list doesn't mean it should.
Prefer immutability whenever possible: only change inputs when there is a clear advantage over making a copy
Clearly define and document the intent of your functions.
(The Python Way)
a = 7
b = 2
print(a, b)
a, b = b, a
print(a, b)
NOTES
numbers = list(range(1, 4))
print(numbers)
numbers = list(range(1, 4))
print(numbers)
more_numbers = []
for i in range(10):
i_mod = i % len(numbers)
next_number = numbers[i_mod]
more_numbers.append(next_number)
print(more_numbers)
i |
i_mod |
numbers[i_mod] |
---|---|---|
0 | 0 | 1 |
1 | 1 | 2 |
2 | 2 | 3 |
3 | 0 | 1 |
4 | 1 | 2 |
5 | 2 | 3 |
6 | 0 | 1 |
7 | 1 | 2 |
8 | 2 | 3 |
9 | 0 | 1 |
NOTES
[]
range
%
) and indexing